package net.huashitong.Indicators.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.entity.HotWord;
import net.huashitong.Indicators.entity.Industry;
import net.huashitong.Indicators.filter.HotWordFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class HotWordDao extends StringDao<HotWord> {

    /**
     * 条件查询热词
     * @param filter
     * @return
     */
    public List<HotWord> queryHotWords(HotWordFilter filter){
        GenericQuery query = createQuery("obj");
        if(ObjectUtils.isNotBlank(filter.getHotWord())){
            query.append(" and obj.hotWord = :hotWord ").setParam("hotWord", filter.getHotWord());
        }
        if(ObjectUtils.isNotBlank(filter.getHotCode())){
            query.append(" and obj.hotCode = :hotCode ").setParam("hotCode", filter.getHotCode());
        }
        return query.listResult();
    }

    public List<HotWord> queryRandom(){
        String hql = "SELECT HOT_CODE as \"hotCode\"," +
                " HOT_WORD as \"hotWord\" FROM (select HOT_WORD,HOT_CODE from BA02 order by dbms_random.value) WHERE rownum <= 10";
        SQLQuery query = getSession().createSQLQuery(hql);
        query.addEntity(HotWord.class);
        List<HotWord> res =( List<HotWord>) query.list();
        return res;
    }
}
